/**
 * @file audio.h
 *
 */


#ifndef __AUDIO_H__
#define __AUDIO_H__


#include <LPC23xx.H>
#include <math.h>
#include "irq.h"


#define max_notes 36

extern void gpio_InitAudioPins(void);
extern unsigned char aud_note_on;		// flag to turn on tone
extern unsigned char aud_note_level;	// Note level 
extern volatile unsigned short aud_mscount; // 1ms counter
extern unsigned char aud_active_note;	// Current active note
extern unsigned char aud_pitch;		// current pitch level 
extern float const AUD_FREQ[max_notes ][5];
extern float const AUD_FREQ1[max_notes ][5];


extern char notacija[max_notes][4];

/**
 * Envelope generator parameters
 */
// define rise and fall time of tones
// decrease to make slower, increase to make faster
#define HP_RISE		12	/// rise time for high priority tones (12 = ~20ms Tr)					
#define HP_FALL		12	/// fall time for high priority tones (12 = ~20ms Tf)
#define MP_RISE		2	/// rise time for medium priority tones (8 = ~30ms Tr)
#define MP_FALL		2	/// fall time for medium priority tones (8 = ~30ms Tf)


/** Pitch level identifiers */
enum 
{
	AUD_HIGH=1,
	AUD_MID,
	AUD_LOW
};

/**
 * Tone generator parameters
 */
#define Amp 200                     // Output Amplitute     
#define PI  3.1415926
#define Fsample  25000             // Timer  Reload Frequency               


/** Structure used with tone generator */
typedef struct {                    // struct for Sine Wave Generator Signal   
  short coef;                      	// IIR filter coefficient                
  long y1;                     		// y[-1] value                            
  long y2;                        	// y[-2] value      
} twave;  



// Function prototypes 


/**
 * IRQ_Timer1Init
 */
int IRQ_Timer1Init( void );


/**
 * Timer1 ISR handler
 */ 
void IRQ_Timer1_Handler(void) __irq;


/**
 * Audio Envelope Control
 */
void aud_EnvelopeControl(void);


void aud_TurnOnNote(void);


/**
 * Init tone coefficients array
 */
void aud_InitToneCoefArray(void);   	 

/**
 * Init tone coefficients array
 */
void aud_InitToneCoefArray1(unsigned long note);   	 



/**
 * Mulitple Sine Wave Generation, Summing, and DAC Output
 */
void aud_GenerateMultiTone (twave *t);  



/**
 * Output tone
 */
void aud_OutputTones(unsigned char note, unsigned char level);


/**
 * Audio system init
 */
int aud_init(void);

#endif

